package cn.sylinx.horm.example.section3;

import java.io.Serializable;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import cn.sylinx.horm.core.SqlClient;
import cn.sylinx.horm.dialect.fs.FS;
import cn.sylinx.horm.example.entity.MyExample;

@Profile("section3")
@Component
public class Section3 implements CommandLineRunner {

    private SqlClient sqlClient;

    public Section3(SqlClient sqlClient) {
        this.sqlClient = sqlClient;
    }

    @Override
    public void run(String... args) throws Exception {
        delete();
    }

    void delete() {
        int deleteCount = sqlClient.delete(FS.of(MyExample.class).where(MyExample::getId).eq(7));
        System.out.println("deleteCount -> " + deleteCount);
        MyExample result = sqlClient.queryFirst(FS.of(MyExample.class).where(MyExample::getId).eq(7));
        System.out.println("Query MyExample result -> " + result);
    }

    void updateWithOptLock() {
    }

    void query() {
        MyExample result = sqlClient.queryFirst(FS.of(MyExample.class).where(MyExample::getName).like("name"));
        System.out.println("Query MyExample result -> " + result);

        result = sqlClient.queryFirst(FS.of(MyExample.class).where(MyExample::getNumber).eq("fs_number"));
        System.out.println("Query MyExample result -> " + result);
    }

    void update() {
        int updateCount = sqlClient.update(FS.of(MyExample.class).set(MyExample::getName, "fs_name_updated")
                .where(MyExample::getNumber).eq("fs_number"));
        System.out.println("Update MyExample count -> " + updateCount);

        MyExample result = sqlClient.queryFirst(FS.of(MyExample.class).where(MyExample::getNumber).eq("fs_number"));
        System.out.println("Query MyExample result -> " + result);
    }

    void create() {
        Serializable pk = sqlClient.insert(
                FS.of(MyExample.class).set(MyExample::getNumber, "fs_number").set(MyExample::getName, "fs_name"));
        System.out.println("MyExample insert pk -> " + pk);

        MyExample result = sqlClient.get(pk, MyExample.class);
        System.out.println("MyExample result -> " + result);
    }
}
